//
//  JJSpringAnimationViewController.m
//  MyAnimations
//
//  Created by jxf on 15/12/23.
//  Copyright © 2015年 JJ.sevn. All rights reserved.
//

#import "JJSpringAnimationViewController.h"

@interface JJSpringAnimationViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIView *greenView;
@property (weak, nonatomic) IBOutlet UIView *darkRedView;

@end

@implementation JJSpringAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.darkRedView.layer.borderWidth = 20;
    self.darkRedView.layer.borderColor = [UIColor clearColor].CGColor;
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)backHome:(UIBarButtonItem *)sender {
 
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (IBAction)action1:(UIBarButtonItem *)sender {
    [self animation1];
}
- (IBAction)action2:(UIBarButtonItem *)sender {
    [self animation2];
}
- (IBAction)action3:(UIBarButtonItem *)sender {
    [self animation3];
}

- (void)animation1
{
    CASpringAnimation *anim = [CASpringAnimation animation];
    anim.keyPath = @"transform.scale";
    anim.damping = 10;
    anim.fromValue = @1.25;
    anim.toValue = @1.0;
    anim.initialVelocity = 0;
    anim.mass = 10.0;
    anim.stiffness = 100;
    anim.duration = anim.settlingDuration;
    [self.redView.layer addAnimation:anim forKey:nil];
}
- (void)animation2
{
    CASpringAnimation *anim = [CASpringAnimation animationWithKeyPath:@"position.x"];
    // mass:质量,越大,惯性越大,弹簧拉升和压缩的幅度就越大
    anim.mass = 1;
    // 刚度系数(劲度系数/弹性系数),越大则产生的形变越大,运动越快
    anim.stiffness = 20;
    // 阻尼系数,组织弹簧伸缩的系数,越大,弹簧越快减速停止
    anim.damping = 50;
    // 初始速率,速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    anim.initialVelocity = 0;
    // 动画结算时间,返回弹簧动画到停止时的估算时间，根据当前的动画参数估算,通常弹簧动画的时间使用结算时间比较准确
    anim.duration = anim.settlingDuration;
    
    anim.toValue = @300;
    [self.greenView.layer addAnimation:anim forKey:nil];
}
- (void)animation3
{
    CASpringAnimation *anim = [CASpringAnimation animationWithKeyPath:@"borderColor"];
    anim.mass = 10;
    anim.stiffness = 1000;
    anim.initialVelocity = 0;
    anim.damping = 10;
    anim.duration = anim.settlingDuration;
    anim.fromValue = (id)[UIColor redColor].CGColor;
    anim.toValue = (id)[UIColor greenColor].CGColor;
    [self.darkRedView.layer addAnimation:anim forKey:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
